<?php

/*

Process Item Helper (item_helper.pro.php)

*/
ob_start();
$rank_check = 1;
include "global.inc.php";

$find_items2 = mysql_query("SELECT * FROM usersitems2 WHERE owner = '$userid' AND game = '$game'");
while ($find_items = mysql_fetch_array($find_items2))
{
	$id = $find_items[id];

	$findRealItem = fetch("SELECT * FROM items2 WHERE id = '$find_items[item_id]' AND game = '$game'");

	if ($use[$id] == "stock")
	{
		if ($find_items[parts_left] == $findRealItem[parts])
		{
			$find_shop = fetch("SELECT * FROM usershops2 WHERE owner = '$userid' AND game = '$game'");
			$sumItems = fetch("SELECT sum(stock) AS stock FROM usershops_items2 WHERE owner = '$userid' AND game = '$game'");

			if ($find_shop[id] == "")
			{
				die("You do not have a shop yet!$closeHTML");
			}

			$max_stock = $find_shop[shop_size] * 5;

			if ($sumItems[stock] >= $max_stock)
			{
				die("There is not enough room for your $find_item2[item_name] in your shop.$closeHTML");
			}

			$findInShop = fetch("SELECT stock FROM usershops_items2 WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			if ($findInShop[stock])
			{
				mysql_query("UPDATE usershops_items2 SET stock=$findInShop[stock]+1 WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			}
			else
			{
				mysql_query("INSERT INTO usershops_items2 (owner,item_id,stock,game) VALUES ('$userid','$find_items[item_id]','1','$game')");
			}
			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
		}
		if ($find_items[parts_left] != $findRealItem[parts])
		{
			$extra .= "<br>Your $findRealItem[item_name] could not be added because it has already been used.";
		}
	}
	if ($use[$id] == "gallery")
	{
		if ($find_items[parts_left] == $findRealItem[parts])
		{
			$find_gallery = fetch("SELECT * FROM usergallerys WHERE owner = '$userid' AND game = '$game'");
			$sumItems = fetch("SELECT sum(stock) AS stock FROM usergallerys_items WHERE owner = '$userid' AND game = '$game'");

			if ($find_gallery[id] == "")
			{
				die("You do not have a gallery yet!$closeHTML");
			}

			$max_stock = $find_gallery[gallery_size] * 5;

			if ($sumItems[stock] >= $max_stock)
			{
				die("There is not enough room for your $find_item2[item_name] in your gallery.$closeHTML");
			}

			$findIngallery = fetch("SELECT stock FROM usergallerys_items WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			if ($findIngallery[stock])
			{
				mysql_query("UPDATE usergallerys_items SET stock=$findIngallery[stock]+1 WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			}
			else
			{
				mysql_query("INSERT INTO usergallerys_items (owner,item_id,stock,game) VALUES ('$userid','$find_items[item_id]','1','$game')");
			}
			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
		}
		if ($find_items[parts_left] != $findRealItem[parts])
		{
			$extra .= "<br>Your $findRealItem[item_name] could not be added because it has already been used.";
		}
	}
	if ($use[$id] == "discard")
	{
		mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
	}
	if ($use[$id] == "donate")
	{
		mysql_query("INSERT INTO penny_store2 (item_id,donated_by,game) VALUES ('$find_items[item_id]','$userid','$game')");
		mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
	}
	if ($use[$id] == "give")
	{
		$give_to = stripspaces(strtolower($give_to));

		$findGiveTo = fetch("SELECT username,id FROM members2 WHERE username = '$give_to' AND game = '$game'");
		if ($findGiveTo[username])
		{
			mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$findGiveTo[id]','$find_items[item_id]','$findRealItem[parts]','$game')");
			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
			$givenItems++;
		}
	}
	if ($use[$id] == "deposit")
	{
		$findRealItem = fetch("SELECT * FROM items2 WHERE id = '$find_items[item_id]' AND game = '$game'");
		if ($find_items[parts_left] == $findRealItem[parts])
		{
			$findIngallery = fetch("SELECT stock FROM safety_box WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			if ($findIngallery[stock])
			{
				mysql_query("UPDATE safety_box SET stock=$findIngallery[stock]+1 WHERE owner = '$userid' AND item_id = '$find_items[item_id]' AND game = '$game'");
			}
			else
			{
				mysql_query("INSERT INTO safety_box (owner,item_id,stock,game) VALUES ('$userid','$find_items[item_id]','1','$game')");
			}
			mysql_query("DELETE FROM usersitems2 WHERE id = '$find_items[id]' AND owner = '$userid' AND game = '$game'");
		}
		if ($find_items[parts_left] != $findRealItem[parts])
		{
			$extra .= "<br>Your $findRealItem[item_name] could not be deposited because it has already been used.";
		}
	}
}

if (($give_to) AND ($givenItems >= "1"))
{
	systemmessage("$game", "myitems.php?game=$game",$findGiveTo[id],"$display_name has given you $givenItems items!");
}

header(error("item_helper.php?game=$game","All commands have been executed successfully.$extra"));

?>